#### Tables S8 and S9: Support for norms/violence by week (panel) ####

# Libraries
# library(tidyverse)
# library(rio)
# library(here)
# library(fixest)
# library(lubridate)

# data_pnas = import(here("Data","data_pnas.rds"))

in_vars = c("norm_judgesre","norm_loyaltyre","norm_pollingre",
            "norm_executivere","norm_censorshipre", paste0("violence",3:6,"re"))

nonpanel_reg = \(x){
  # Run regressions
  mod_stability = feols(fml = as.formula(paste0(x," ~ week_since")),
                        data = data_pnas, weights = data_pnas$weight*data_pnas$wt_adj)
  #Output dataframe
  lst_stability = lst(type = "stability",
                      depvar = x,
                      mod = mod_stability)
  lst(lst_stability)
}

etab_create = \(out, vars, type, file){
  xtrct = map(vars, \(y){
    out[[y]][[type]][["mod"]]
  })
  etable(xtrct, digits = 2, file = file, replace = T,
         signif.code = c("***"=0.001, "**"=0.01, "*"=0.05))
}

reg_out = map(in_vars, ~ nonpanel_reg(.x))
names(reg_out) = in_vars

etab_create(reg_out,
            c("norm_judgesre","norm_loyaltyre","norm_pollingre",
              "norm_executivere","norm_censorshipre"),
            "lst_stability", here("Tables","Supplementary","table_s8.tex")) 

etab_create(reg_out,
            c(paste0("violence",3:6,"re")),
            "lst_stability", here("Tables","Supplementary","table_s9.tex")) 